const app = getApp();
const rtcroom = require('../../utils/rtcroom.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    logo: '',
    close: '../../Resources/common/common-05.png',
    sysName: '',
    callSubtitle: '',
    message: '',
    clock: '',
    timeIntervalID: null,
    callingTimer: null,
    userName: '',
    canCall: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userName: options.userName || '',
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setTime();
    this.getPoliceCount();
    this.getBaseInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.intervalID);
    clearTimeout(this.data.callingTimer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.intervalID);
    clearTimeout(this.data.callingTimer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  setTime: function () {
    let minute = 0;
    let second = 0;
    let total = 0;
    if (!this.data.timeIntervalID) {
      const timeIntervalID = setInterval(() => {
        total += 1;
        second += 1;
        if (second >= 60) {
          second = 0;
          minute += 1;
        }
        if (total > 60) {
          this.setData({
            message: '系统繁忙，可点此拨打客服电话',
            canCall: true,
          });
        }
        this.setData({
          clock: `${`0${minute}`.slice(-2)}:${`0${second}`.slice(-2)}`,
        });
      }, 1000);
      this.setData({
        timeIntervalID,
      });
    }
  },

  getPoliceCount: function () {
    const self = this;
    app.util.request({
      url: 'entry/wxapp/index',
      method: 'POST',
      data: {
        op: 'policeOnline',
      },
      module: 'cm_video_alarm',
      success: function (res) {
        if (res.data && res.data.errno == 0) {
          self.setData({
            policeCount: res.data.data && res.data.data.total,
          });
          self.getRoomList();
        }
      },
      fail: function (res) {
        self.setData({
          message: '系统错误，可点此拨打客服电话',
          canCall: true,
        });
      },
    });
  },

  call110: function () {
    if (this.data.canCall) {
      wx.makePhoneCall({
        phoneNumber: this.data.phonenumber,
      });
    }
  },

  callCancel: function () {
    clearInterval(this.data.intervalID);
    clearTimeout(this.data.callingTimer);
    app.util.navigateTo('/cm_video_alarm/index/index');
  },

  getRoomList: function() {
    let self = this;
    rtcroom.getRoomList({
      data: {
        index: 0,
        cnt: self.data.policeCount,
      },
      success: function(res) {
        clearTimeout(self.data.callingTimer);
        if (res.rooms && (res.rooms.length < self.data.policeCount || res.rooms.length < 10)) {
          const userName = self.data.userName;
          const roomName = `${userName}`;
          const url = `/cm_video_alarm/trtc/pages/tourist-room/tourist-room?type=create&roomName=${roomName}&userName=${userName}`;
          app.util.navigateTo(url, {}, true);
        } else {
          const callingTimer = setTimeout(() => {
            self.getRoomList();
          }, 5000);
          self.setData({
            callingTimer,
          });
        }
      },
      fail: function(res) {
        console.log(res);
        self.setData({
          message: '系统错误，可点此拨打客服电话',
          canCall: true,
        });
      },
    });
  },

  getBaseInfo() {
    let { 
      call_title: sysName = '',
      call_logo: logo = '',
      call_description: callSubtitle = '',
      call_hint: message = '',
      phonenumber = '',
    } = wx.getStorageSync('alarmBaseInfo');

    this.setData({
      sysName,
      callSubtitle,
      message,
      phonenumber,
      logo,
    });
  },
});
